<?php
namespace app\admin\controller;

use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Model;


/**
 * 广告管理
 */
class Banner extends Base
{
    /**
     * @throws
     * 广告列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Model('banner');
        $list = $model->where('is_del',0)->where('type',0)->order("id desc")->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * @throws
     * 广告回收站
     */
    public function index_del()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Model('banner');
        $list = $model->where('is_del',1)->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * 添加广告
     */
    public function add()
    {
        if(request()->isPost()){

            $model = Model('banner');

            // 图片资源
            $file   = request()->file('pic_url');

            if(!$file){
                $this->error("广告图片未上传");
            }

            // 本地路径
            $filePath = $file->getRealPath();

            // 文件后缀
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

            // 七牛存储文件名
            $file_name = 'images/banner/'.date("Ymd").rand(1000,9999).'.'.$ext;

            // 实例化七牛云
            $qiniu = new Qiniu();

            // 上传至七牛云
            $re = $qiniu->upload_pic($filePath,$file_name);
            if($re['code'] == 0){
                $this->error("图片未成功上传七牛云".$re['data']);
            }

            // 存入数据库
            $data['title']      = input("title");
            $data['url']        = input("url",'/');
            $data['pic_url']    = $re["data"];
            $data['start_time'] = strtotime(input('start_time'));
            $data['end_time']   = strtotime(input('end_time'));

            $ret =$model->insert($data);
            if($ret){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }

        }

        return $this->fetch();
    }


    /**
     * @throws
     * 编辑广告
     */
    public function edit()
    {
        $id = input('id');

        $model = Model('banner');

        // 更新数据
        if(request()->isPost()){

            $data = input('');

            // 图片是否被编辑
            $file   = request()->file('pic_url');
            if($file){
                // 本地路径
                $filePath = $file->getRealPath();

                // 文件后缀
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

                // 七牛存储文件名
                $file_name = 'images/banner/'.date("Ymd").rand(1000,9999).'.'.$ext;

                // 实例化七牛云
                $qiniu = new Qiniu();

                // 上传至七牛云
                $re = $qiniu->upload_pic($filePath,$file_name);
                if($re['code'] == 0){
                    $this->error("图片未成功上传七牛云".$re['data']);
                }

                $data['pic_url']    = $re["data"];

            }

            input('start_time')     == null || $data['start_time']      = strtotime(input('start_time'));
            input('end_time')       == null || $data['end_time']        = strtotime(input('end_time'));

//            if(count($data)<=0){
//                $this->error("未做任何修改");
//            }

            $re = $model->where('id',$id)->update($data);
            if($re===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");
        }

        // 详情
        $info = $model->where('id',$id)->find();
        $this->assign('info',$info);

        return $this->fetch();
    }


    
}
